#!/bin/bash

# Copyright (c) 2024 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

function prepare_flutter_env() {
    if [ ! -d "flutter_env" ]
    then
        git clone https://gitee.com/getmoon/flutter_env.git
        if [ $? -ne 0 ]
        then
            echo "clone flutter_env failed!"
            rm -rf flutter_env
            return 1
        fi
    fi

    return 0
}

function prepare_flutter_flutter() {
    if [ ! -d "flutter_flutter" ]
    then
        git clone https://gitcode.com/openharmony-sig/flutter_flutter.git
        if [ $? -ne 0 ]
        then
            echo "clone flutter_flutter failed!"
            rm -rf flutter_flutter
            return 1
        else
            if [ "$(git -C flutter_flutter branch | grep dev)" == "" ];
            then
                echo "flutter_flutter create branch dev"
                git -C flutter_flutter checkout -b dev origin/dev
            else
                echo "flutter_flutter checkout dev"
                git -C flutter_flutter checkout dev
            fi

        fi
    fi

    git -C flutter_flutter pull

    return 0
}

function prepare_commandlinetools(){
    echo "prepare_commandlinetools"

    if [ -d "command-line-tools" ];then
        echo "command-line-tools exists, do not download again."
    else
        if [ ! -f "command-line-tools.zip" ];then
            echo "start download command-line-tools.zip"
            curl -o "command-line-tools.zip" https://repo.huaweicloud.com/harmonyos/ohpm/5.0.5/commandline-tools-linux-x64-5.0.5.310.zip

            # 判断是否下载成功
            if [ $? != 0 ];then
                echo "download command-line-tools.zip failed."
                rm -rf command-line-tools.zip
                return 1
            fi
        fi

        # 开始解压zip包
        echo "start unzip command-line-tools.zip"
        unzip -oqq "./command-line-tools.zip"

        echo "finish unzip command-line-tools.zip"
    fi

    return 0
}

function prepare() {
    apt-get update && apt-get install -y curl tar unzip python3 tree libc6-dev libc6 binutils wget gcc make gawk bison && apt-get remove -y nodejs

    prepare_flutter_env
    if [ $? -ne 0 ]
    then
        echo "prepare_flutter_env failed!"
        return 1
    fi

    prepare_flutter_flutter
    if [ $? -ne 0 ]
    then
        echo "prepare_flutter_flutter failed!"
        return 1
    fi

    prepare_commandlinetools
    if [ $? -ne 0 ]
    then
        echo "prepare_commandlinetools failed!!"
        return 1
    fi

	  #创建npmrc文件
    rm -f ~/.npmrc
    echo -e "registry=https://repo.huaweicloud.com/repository/npm/\n@ohos:registry=https://repo.harmonyos.com/npm/\nlockfile=false" > ~/.npmrc
    rm -rf ~/.ohpm
    mkdir ~/.ohpm
    touch ~/.ohpm/.ohpmrc
    echo -e "registry=https://ohpm.openharmony.cn/ohpm/" > ~/.ohpm/.ohpmrc

    rm -f compile.sh
    cp cicd/flutter/packages/compile.sh .

    rm -rf hap_out
    mkdir hap_out

    echo "-->Finish prepare.sh."

    return 0
}

# shellcheck disable=SC2068
prepare $@

exit $?

#eof